/**
 * Class: PControls
 *
 * Purpose: 
 *
 * Created: Feb 17, 2010 @ 11:21:57 AM
 * @author Ken Scott
 */
package org.openfantasia.songanalyst.controls;

import org.openfantasia.songanalyst.SongObserver;
import org.openfantasia.ui.Layout;
import org.openfantasia.ui.PFrame;
import org.openfantasia.util.listener.DoubleScalar;
import org.openfantasia.util.listener.Listener;
import org.openfantasia.util.listener.Event;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import java.awt.*;
import java.text.NumberFormat;

public class PControls extends JComponent {

	public static final NumberFormat DOUBLE_FORMAT = NumberFormat.getInstance();


	static {
		DOUBLE_FORMAT.setMaximumFractionDigits(3);
	}


	protected SongObserver home;

	protected PowerDisplay powerDisplay;

	protected PlaybackControls playbackControls;

	protected JLabel timeDisplay;


	public PControls(final SongObserver home, final int height) {
		this.home = home;
		setSize(home.width, height);
		setBorder(new SoftBevelBorder(BevelBorder.RAISED));

		powerDisplay = new PowerDisplay(0.1f, Color.WHITE, Color.BLUE, Color.RED);
		add(powerDisplay);
		home.addListener(SongObserver.SMOOTH_AVERAGE, powerDisplay);

		timeDisplay = new JLabel();
		timeDisplay.setHorizontalTextPosition(JLabel.CENTER);
		timeDisplay.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
		add(timeDisplay);
		home.addListener(SongObserver.TIME, new Listener() {
			public void receive(Event event) {
				double time = ((DoubleScalar) event).getValue();
				timeDisplay.setText(DOUBLE_FORMAT.format(time));
			}
		});

		playbackControls = new PlaybackControls(home);
		add(playbackControls);
		home.addListener(SongObserver.TIME, playbackControls);
		home.addListener(SongObserver.TO_START, playbackControls);
		home.addListener(SongObserver.TO_END, playbackControls);
		home.addListener(SongObserver.PLAY, playbackControls);
		home.addListener(SongObserver.STOP, playbackControls);
		home.addListener(SongObserver.BACK, playbackControls);
		home.addListener(SongObserver.FFWD, playbackControls);
		home.addListener(SongObserver.FREV, playbackControls);


		Point p = home.getLocationOnScreen();
		setLayout(new Layout(this) {

			public Dimension preferredLayoutSize(Container parent) {
				return new Dimension(home.width, height);
			}


			public Dimension minimumLayoutSize(Container parent) {
				return new Dimension(home.width, height);
			}


			public void layoutContainer(Container parent) {
				Dimension dims = parent.getSize();

				powerDisplay.setBounds(2, 2, 200, dims.height - 26);
				timeDisplay.setBounds(2, dims.height - 26 + 2, 200, 22);
				int powerDisplayWidth = powerDisplay.getX() + powerDisplay.getWidth();
				int playbackControlMarginH = 2;
				playbackControls.setBounds(powerDisplayWidth + playbackControlMarginH, 2, parent.getWidth() - powerDisplayWidth - (2 * playbackControlMarginH), (dims.height - 4));
			}
		});

		setVisible(true);
	}


	public PFrame getHome() {
		return home;
	}


	public void setHome(SongObserver home) {
		this.home = home;
	}
}
